package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.system.AppSettings;

/**
 * test
 * @author normenhansen
 */
public class Main extends SimpleApplication {

    public static void main(String[] args) {
        Main app = new Main();
        AppSettings settings = new AppSettings(true);
        settings.setRenderer(AppSettings.LWJGL_OPENGL2);
        settings.setResolution(1280,720);
        settings.setVSync(true);//Matched to real feeling of 60fps not over 9000
        settings.setTitle("I LIKE TURTLES!");
        app.setSettings(settings);
        app.setShowSettings(false);
        app.start();
    }

private Node pivot;
 
    @Override
    public void simpleInitApp() {
    	pivot = new Node("pivot");
    	rootNode.attachChild(pivot);
    	
        Spatial teapot = assetManager.loadModel("Models/a.obj");
        teapot.scale(0.03f, 0.03f, 0.03f);
        rootNode.attachChild(teapot);
        
        Spatial t = assetManager.loadModel("Models/test.obj");
        pivot.attachChild(t);

        // You must add a light to make the model visible
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
        rootNode.addLight(sun);
 		
        initKeys();
        cam.setFrustumPerspective(45.0f,1.77f,1.0f,100000.0f);
        cam.update();
    }
    
    private void initKeys() {
        // You can map one or several inputs to one named action
    	inputManager.clearMappings();
        inputManager.addMapping("Forward",  new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("Left",   new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("Right",  new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("Backward", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("Up",  new KeyTrigger(KeyInput.KEY_R));
        inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_F));
        inputManager.addMapping("RLeft",  new KeyTrigger(KeyInput.KEY_Q));
        inputManager.addMapping("RRight", new KeyTrigger(KeyInput.KEY_E));
        // Add the names to the action listener.
        inputManager.addListener(analogListener, new String[]{"Up","Left", "Right", "Down","Forward","Backward","RLeft","RRight"});
     
	}
    
    private AnalogListener analogListener = new AnalogListener() {
      public void onAnalog(String name, float value, float tpf) {
    	  //Need to use camera object instead of the land
	      if (name.equals("Forward")) {
	    	  pivot.move(0.0f, 0.0f,1.0f);
	      }
	      else if (name.equals("Backward")) {
	    	  pivot.move(0.0f,0.0f,-1.0f);
	      }
	      else if (name.equals("Left")) {
	    	  pivot.move(1.0f,0.0f, 0.0f);
	      }
	      else if (name.equals("Right")) {
	    	  pivot.move(-1.0f,0.0f, 0.0f);
	      }
	      else if (name.equals("Up")) {
	    	  pivot.move(0.0f, -1.0f,0.0f);
	      }
	      else if (name.equals("Down")) {
	    	  pivot.move(0.0f,1.0f,0.0f);
	      }
	      else if (name.equals("RLeft")) {
	    	  pivot.rotate( 0.0f,-0.01f,0.0f);
	      }
	      else if (name.equals("RRight")) {
	    	  pivot.rotate( 0.0f,0.01f, 0.0f);
	      }
      }
    };

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
        rm.setAlphaToCoverage(true);
    }
}
